package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("login")//登录
    public UserBean login(String userName, String pwd, HttpServletRequest request) {
        UserBean user = userService.login(userName, pwd);
        //得到主体
        Subject subject = SecurityUtils.getSubject();
        try {
            //登录完成后，shiro会将登录用户绑定在session中
            subject.login(new UsernamePasswordToken(userName, pwd));
            //得到登录用户对象
//            UserBean us=(UserBean)subject.getPrincipal();
            if (user != null) {
                request.getSession().setAttribute("loginUser", user);
            }

        }catch (Exception e){
            return user;
        }
        return user;
    }

    @RequestMapping("updatePwd")//修改当前登录用户修改密码
    public String updatePwd(String newPwd, HttpServletRequest request) {//
        UserBean user = (UserBean) request.getSession().getAttribute("loginUser");//得到当前登录用户的对象
        userService.updatePwd(user.getId(), newPwd);
        return "ok";
    }
    @RequiresPermissions("user")
    @RequestMapping("findByItem")//根据用户名动态分页查询用户表
    public IPage<UserBean> findByItem(Integer pageNO, String name) {
        return userService.findByItem(pageNO, name);
    }
    @RequiresPermissions("user")
    @RequestMapping("findByName")
    public String findByName(String name) {//添加用户时，用户名不能重复.重复返回no，不重复返回ok
        boolean flag = userService.findByName(name);
        if (flag != true) {
            return "ok";
        }
        return "no";
    }
    @RequiresPermissions("user")
    @RequestMapping("add")
    public String add(UserBean userBean, int [] role){//添加用户信息，同时添加该用户拥有的角色信息（中间表）
        if (role.length!=0) {
            userService.add(userBean,role);
            return "ok";
        }else {
            System.out.println("空的");
            return "no";
        }

    }
    @RequiresPermissions("user")
    @RequestMapping("del")
    public String del(Integer id){//删除用户
        userService.del(id);
        return "ok";
    }
    @RequiresPermissions("user")
    @RequestMapping("findById")//通过id查询用户信息，同时查询该用户拥有的角色集合，和权限集合
    public UserBean findById(Integer id){
        UserBean user=userService.findById(id);
        return user;
    }
    @RequiresPermissions("user")
    @RequestMapping("updateInfo")//通过用户id修改用户的角色权限
    public String updateInfo( int [] arrays ,Integer id){
        if(arrays.length!=0){
            userService.updateInfo(id,arrays);
            return "ok";
        }else {
            return "no";
        }

    }
}
